package top.yunmouren.areamusic.event;

import org.bukkit.Bukkit;
import top.yunmouren.areamusic.AreaMusic;
import top.yunmouren.areamusic.config.AreaDto;
import top.yunmouren.areamusic.exception.PlayerNullException;
import top.yunmouren.areamusic.player.PlayerBridge;
import top.yunmouren.areamusic.utils.MusicUtil;

import java.util.List;

public class PeriodCheckRunnable extends MusicUtil implements Runnable {
    private final AreaMusic plugin;
    public PeriodCheckRunnable(AreaMusic plugin) {
        super(plugin);
        this.plugin = plugin;
    }
    public void run() {
        Bukkit.getOnlinePlayers().forEach((player) -> {
            try {
                PlayerBridge playerBridge = new PlayerBridge(player, plugin);
                this.freshTag(playerBridge);
                if (playerBridge.getPlayerMusicTag() == null) {
                    List<AreaDto> insideList = this.getInsideList(player.getWorld().getName(), player.getLocation().getX(), player.getLocation().getY(), player.getLocation().getZ());
                    if (insideList.size() == 0) {
                        return;
                    }
                    AreaDto max = this.selectMostImportantArea(insideList);
                    this.play(playerBridge, max);
                }
            } catch (PlayerNullException ignored) {
            }
        });

    }
}